﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Model;
using MsDAL;
using System.ComponentModel;
using Components;

namespace ServiceHelper
{
    [DataObjectAttribute]
    public class ImportManager
    {
        private static ImportManager _entity;

        public static ImportManager Entity
        {
            get
            {
                if (_entity == null)
                    _entity = new ImportManager();
                return _entity;
            }
        }

        #region 个人代码

        /// <summary>
        /// 做导入动作
        /// </summary>
        /// <param name="tb"></param>
        /// <param name="dtl"></param>
        /// <param name="deldata"></param>
        /// <param name="action"></param>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public string ImportData(Hashtable tb, DataTable dtl, DataTable deldata, string action, string usercode)
        {
            if (DataTableHelper.Entity.IsEmptyTable(dtl)) return "没有可供导入的数据！";
            List<SqlApp> lst = new List<SqlApp>();

            //公司代码
            string comde = Validation.IsNull(tb["comcode"]) ? "XXX" : tb["comcode"].ToString();

            //先清空临时表
            lst.Add(ImportHelperDAL.Entity.DeleteI11073(true));

            //将要插入的数据导入临时表
            I11073 model = null;
            foreach (DataRow dr in dtl.Rows)
            {
                try
                {
                    model = new I11073();
                    Components.CommonUtil.GetInstance.CopyProperties(model, dr);

                    lst.Add(I11073Manager.Entity.Add(model));
                }
                catch 
                {
                    return string.Format("商品编码({0})所在行数据有问题！",Validation.ToString(dr["code"]));
                }
            }

            lst.Add(ImportHelperDAL.Entity.ImportProdct(comde));

            return CommonManager.Entity.SaveData(lst);
        }

        /// <summary>
        /// 导入商品模块的验证商品数据功能
        /// </summary>
        /// <param name="tb"></param>
        /// <param name="dtl"></param>
        /// <param name="deldata"></param>
        /// <param name="action"></param>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public string CheckProducts(Hashtable tb, DataTable dtl, DataTable deldata, string action, string usercode)
        {
            if (null == dtl || 0 == dtl.Rows.Count) return "没有数据可供验证！";

            DataRow dr = dtl.Rows[0];

            List<SqlApp> savedata = new List<SqlApp>();
            Hashtable where = new Hashtable(); //条件集合
            //公司代码
            string comde = Validation.IsNull(tb["comcode"]) ? "XXX" : tb["comcode"].ToString();
            string msg;
            string checkFun = ""; //验证函数
            string labelName = "";
            foreach (DataColumn dc in dtl.Columns)
            {
                savedata.Clear();
                savedata.AddRange(BatchForTmp(Validation.ToString(dr[dc.ColumnName])));
                CommonManager.Entity.SaveData(savedata); //执行到数据库
                where.Clear();

                switch (dc.ColumnName)
                {
                    case "brand": //验证品牌
                        where["comcode"] = comde;
                        checkFun = "checkbrand";
                        labelName = "品牌代码";
                        break;
                    case "dw": //单位代码
                        where["comcode"] = comde;
                        checkFun = "checkum";
                        labelName = "单位代码";
                        break;
                    case "dj": //等级代码
                        where["comcode"] = comde;
                        checkFun = "checklever";
                        labelName = "等级代码";
                        break;
                    case "cbfs": //成本方式
                        checkFun = "checkcalc";
                        labelName = "成本方式";
                        break;
                    case "tax": //税率代码
                        where["comcode"] = comde;
                        checkFun = "checktax";
                        labelName = "税率代码";
                        break;
                    case "family": //商品分类代码
                        where["comcode"] = comde;
                        checkFun = "checklevercode";
                        labelName = "分类代码";
                        break;
                    case "po": //商品属性代码
                        where["comcode"] = comde;
                        checkFun = "checkproperty";
                        labelName = "属性代码";
                        break;
                    case "kind": //商品类型代码
                        where["comcode"] = comde;
                        checkFun = "checktype";
                        labelName = "类型代码";
                        break;

                }

                //验证数据库中是否存在
                DataTable result = CommonManager.Entity.ExecDBFun(checkFun, where);
                msg = TranMsg(result, labelName);
                if (!string.IsNullOrEmpty(msg)) return msg;
            }

            return "";
        }

        List<SqlApp> BatchForTmp(string data)
        {
            if (string.IsNullOrEmpty(data)) return null;
            string[] items = data.Split(',');

            List<SqlApp> lst = new List<SqlApp>();

            //先清空#tmp
            lst.Add(ImportHelperDAL.Entity.DeleteTmp(true));

            foreach (string item in items)
            {
                lst.Add(ImportHelperDAL.Entity.InsertTemp(item));
            }

            return lst;
        }

        /// <summary>
        /// 翻译消息
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        string TranMsg(DataTable dt, string title)
        {
            if (dt == null || dt.Rows.Count == 0) return "";

            StringBuilder sb = new StringBuilder();
            foreach (DataRow dr in dt.Rows)
            {
                if (sb.Length != 0) sb.Append(",");
                sb.Append(dr[0]);
            }

            return string.Format("下列『{0}』在目标公司中找不到匹配项，请先在基础模块中添加！\r\n错误代码列表：{1}", title, sb.ToString());
        }

        #endregion
    }
}



